package com.ruoyi.common.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

public class OcrUtil {

    public static String licenceOcr(String url)
    {
        url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnews.cnr.cn%2Fdj%2F20200227%2FW020200227754444270459.png&refer=http%3A%2F%2Fnews.cnr.cn&app=2002&size=f10000,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1719649045&t=b859600b8c4bc686eb92f99c621b87ed";
        String host = "https://bizlicense.market.alicloudapi.com";
        String path = "/rest/160601/ocr/ocr_business_license.json";
        String method = "POST";
        String appcode = "767f6087ad75430a9f87c0205ec81b2f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = "{\"image\":\""+url+"\"}";
        String entity="";
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
             entity = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }


    public static String sendMsg(String phone,String code) {
        String host = "https://zwp.market.alicloudapi.com";
        String path = "/sms/sendv2";
        String method = "GET";
        String appcode = "767f6087ad75430a9f87c0205ec81b2f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("content", "【智能云】您的验证码是"+code+"。如非本人操作，请忽略本短信");
        String entity="";
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //System.out.println(response.toString());
            //获取response的body
            entity = EntityUtils.toString(response.getEntity());
          /*  ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(entity,Map.class);
            String errorCode = map.get("errorCode").toString();*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }

        //手机号格式验证
    public static boolean isPhone(String str) {
        String regex = "^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[0135678])[0-9]{8}$";
        boolean flg = Pattern.matches(regex, str);
        if (flg == false) {
            return false;
        } else {
            return true;
        }
    }


    // 生成指定长度的验证码的方法
    public static String generateVerificationCode(int codeLength) {
        String codeChars = "0123456789";
        StringBuilder verificationCode = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < codeLength; i++) {
            char randomChar = codeChars.charAt(random.nextInt(codeChars.length()));
            verificationCode.append(randomChar);
        }
        return verificationCode.toString();
    }


}





